const source = require('../sources/a.json')
const { downloadContent, parseContent } = require('../modules/content')
const chapter = require('../temp/chapter.json')
const util = require('util')
const fs = require('fs')

const start = 0
const lenght = 100

async function download () {
  if (!fs.existsSync(__dirname + '/../temp/112119/')) {
    fs.mkdirSync(__dirname + '/../temp/112119/')
  }
  for (let i = start; i < lenght; i++) {
    console.log('开始下载：', chapter[i].chapterName)
    const doc = await downloadContent(source, chapter[i].contentUrl)
    const content = await parseContent(source, doc)
    const names = chapter[i].contentUrl.split('/')
    await util.promisify(fs.writeFile)(__dirname + '/../temp/112119/' + names[names.length - 1], content)
    console.log(chapter[i].chapterName, '，下载成功！')
  }
}

download().catch(console.log)
